package com.vtc.session14;

import com.vtc.R;
import com.vtc.session13.Student;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;


public class StudentListActivity extends Activity implements OnItemLongClickListener{

	private ListView lv;
	private StudentAdapter adpater;
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.studentlist1_activity);
		
		lv = (ListView) findViewById(R.id.studentlist_list);
		
		adpater = new StudentAdapter(this);
		adpater.fetchAll();
		
		lv.setAdapter(adpater);
		
		lv.setOnItemLongClickListener(this);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuFlat = getMenuInflater();
		menuFlat.inflate(R.menu.student_menu, menu);	
		return true;
	}
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
	
		switch(item.getItemId()){
		    case R.id.miStAdd:
		    	StudentAddFormActivity frmAdd = new StudentAddFormActivity(this.adpater);
		    	frmAdd.show(getFragmentManager(), "");
		    	break;
		    case R.id.miStDel:		    	
		    	this.adpater.delete();
		    	break;
		}
		return false;
	}
	@Override
	public boolean onItemLongClick(AdapterView<?> av, View arg1, int position,
			long arg3) {
		StudentAdapter ada= (StudentAdapter) av.getAdapter();
		Student st = (Student) ada.getItem(position);
		StudentAddFormActivity frmAdd = new StudentAddFormActivity(this.adpater);
		frmAdd.setValue(st);
    	frmAdd.show(getFragmentManager(), "");
		return false;
	}
}
